package com.irdstudio.efp.nls.service.impl.yed.apply.ctrcont;

import com.irdstudio.basic.framework.core.constant.SDicCertTypeEnum;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.util.Amount2RMB;
import com.irdstudio.efp.console.service.facade.PrdInfoLprService;
import com.irdstudio.efp.console.service.facade.SOrgInfoService;
import com.irdstudio.efp.console.service.facade.SOrgService;
import com.irdstudio.efp.console.service.vo.PrdInfoLprVO;
import com.irdstudio.efp.console.service.vo.SOrgInfoVO;
import com.irdstudio.efp.console.service.vo.SOrgVO;
import com.irdstudio.efp.ctr.service.facade.CtrLoanContService;
import com.irdstudio.efp.ctr.service.vo.CtrLoanContVO;
import com.irdstudio.efp.cus.service.facade.CusIndivService;
import com.irdstudio.efp.cus.service.facade.CusReferrerInfoService;
import com.irdstudio.efp.cus.service.vo.CusIndivVO;
import com.irdstudio.efp.cus.service.vo.CusReferrerInfoVO;
import com.irdstudio.efp.loan.service.facade.AccLoanService;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 需求编号:2019D08--
 * 问题编号:
 * 开发人员: zhangxican
 * 创建日期:2020/3/13
 * 功能描述:
 * 修改日期:2020/3/13
 * 修改描述:
 */
@Component("yedApplyCtrLoanCont")
public class YedApplyCtrLoanCtr extends AbstractCtrLoanCtrBean<NlsApplyInfoVO> {
    //@Value("") 输入路径
    //@Value("") 输入路径
    // 还款方式
    protected String repayMethod;
    // LPR利率
    protected String LPRRate;

    public YedApplyCtrLoanCtr() {
        // 输入路径
        inputPath = "";
        // 输出路径
        outputPath = "";
    }

    @Override
    protected void setSpecifiedLoanContMedaData(NlsApplyInfoVO nlsApplyInfoVO) throws BizException {
        CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
        ctrLoanContVO.setContNo(nlsApplyInfoVO.getApplySeq());
        ctrLoanContVO = ctrLoanContService.queryByPk(ctrLoanContVO);

        CusIndivVO cusIndivVO = new CusIndivVO();
        cusIndivVO.setCusId(nlsApplyInfoVO.getCusId());
        cusIndivVO = cusIndivService.queryByPk(cusIndivVO);

        CusReferrerInfoVO cusReferrerInfoVO = new CusReferrerInfoVO();
        cusReferrerInfoVO.setLmtApplySeq(nlsApplyInfoVO.getLmtApplySeq());
        cusReferrerInfoVO = cusReferrerInfoService.queryChargeoffBrIdByPk(cusReferrerInfoVO);

        if(Objects.nonNull(cusReferrerInfoVO)){
            SOrgVO sOrgVO = new SOrgVO();
            sOrgVO.setOrgCode(cusReferrerInfoVO.getChargeOffBrId());
            sOrgVO = sOrgService.queryByPk(sOrgVO);
            //出账机构
            addContMetaData("chargeoffBrName",Objects.nonNull(sOrgVO)?sOrgVO.getOrgName():"");
        }

        if (Objects.nonNull(cusIndivVO)) {
            //客户住址
            addContMetaData("cusAddr", cusIndivVO.getIndivRsdAddr());
            //客户电话
            addContMetaData("cusPhone", cusIndivVO.getPhone());
        }

        if (Objects.nonNull(ctrLoanContVO)) {
            //客户姓名
            addContMetaData("cusName", ctrLoanContVO.getCusName());
            //证件类型
            addContMetaData("certType", SDicCertTypeEnum.getDesc(ctrLoanContVO.getCertType()));
            //证件号码
            addContMetaData("certCode", ctrLoanContVO.getCertCode());

            // 授信合同号
            addContMetaData("lmtContNo", ctrLoanContVO.getContNo());
        }

        //还款日
        String returnDate = "";
        AccLoanVO accLoanVO = new AccLoanVO();
        accLoanVO.setCertCode(cusIndivVO.getCertCode());
        accLoanVO.setCusId(cusIndivVO.getCusId());
        accLoanVO.setPrdId(nlsApplyInfoVO.getPrdId());
        accLoanVO = accLoanService.queryfirstDisbDateByCond(accLoanVO);
        if (Objects.nonNull(accLoanVO)){
            returnDate = accLoanVO.getReturnDate();
        }else {
            String applyDate = nlsApplyInfoVO.getApplyDate();
            returnDate = applyDate.substring(applyDate.length()-2,applyDate.length());
            returnDate = Integer.valueOf(returnDate)>28 ? "28":returnDate; //还款日大于28日则取28
        }
        addContMetaData("repaymentDay", returnDate);

        String amount = nlsApplyInfoVO.getApplyAmt().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        addContMetaData("lmtAmtLower", amount);
        addContMetaData("lmtAmtUpper", Amount2RMB.convert(amount));
        addContMetaData("contNo", nlsApplyInfoVO.getApplySeq());
        addContMetaData("loanTerm", nlsApplyInfoVO.getLoanTerm());//贷款期限
//        addContMetaData("repayMethod", "101".equals(nlsApplyInfoVO.getRepayMethod()) ? "1" : "2");

        //lpr贷款期限
        String termStr = "";
        String term = "";
        if ("3".equals(nlsApplyInfoVO.getLoanTerm()) ){
            termStr = "三个月至六个月";
            term ="2";

        }else if ("6".equals(nlsApplyInfoVO.getLoanTerm()) ||"12".equals(nlsApplyInfoVO.getLoanTerm())){
            termStr = "六个月至一年（含）";
            term ="2";

        }else if ("24".equals(nlsApplyInfoVO.getLoanTerm()) ||"36".equals(nlsApplyInfoVO.getLoanTerm())){
            termStr = "一年至三年（含）";
            term ="3";
        }

        PrdInfoLprVO prdInfoLprVO = new PrdInfoLprVO();
        prdInfoLprVO.setTerm(term);
        prdInfoLprVO = prdInfoLprService.queryByIntratecdAndTerm2(prdInfoLprVO);
        // (固定利率 - 当月LPR利率）* 10000
        if (prdInfoLprVO != null) {
            BigDecimal loanRate = nlsApplyInfoVO.getLoanRate().multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);
            String lprRate = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).abs().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            //String quoteRates = prdInfoLprVO.getIntrate().setScale(2, BigDecimal.ROUND_HALF_UP).toString()+"%";
            //String operator = loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).signum()==1?"加":"减";
            //基点
            addContMetaData("quoteRates",loanRate.subtract(prdInfoLprVO.getIntrate()).multiply(new BigDecimal("100")).toString());
            //lpr生效日期
            DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
            addContMetaData("expireDate",df.format(prdInfoLprVO.getEffectDate()));

        }

        addContMetaData("term",termStr);

        //签约日期 yyyy-MM-dd
        addContMetaData("signDate", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
        //还款账号
        addContMetaData("recvAcctNo",nlsApplyInfoVO.getRecvAcctNo());
        // 放款卡号
        addContMetaData("recvNo",nlsApplyInfoVO.getDnAccount());
        //开户行名称
        addContMetaData("recvBankName",nlsApplyInfoVO.getBankName());
        //贷款卡开户行全称
        addContMetaData("orgName", nlsApplyInfoVO.getBankName());
        // 年利率
        addContMetaData("yRate", Objects.nonNull(nlsApplyInfoVO.getLoanRate()) ? nlsApplyInfoVO.getLoanRate().multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP).toString() : "0.00");
        // 借款人邮寄地址（分支行机构注册地址）
        SOrgInfoVO pkParam = new SOrgInfoVO();
        pkParam.setInnerOrg(cusReferrerInfoVO.getChargeOffBrId());
        SOrgInfoVO sOrgInfoVO = sOrgInfoService.queryByPk(pkParam);
        if (Objects.isNull(sOrgInfoVO)) {
            throw new BizException("获取分支行机构信息为空");
        }
        if (StringUtils.isEmpty(sOrgInfoVO.getInnerOrg())) {
            throw new BizException("获取分支行机构信息为空");
        }
        addContMetaData("sOrgAddr", sOrgInfoVO.getRegistAddr());
    }

    @Autowired
    @Qualifier("ctrLoanContService")
    private CtrLoanContService ctrLoanContService;
    @Autowired
    @Qualifier("prdInfoLprService")
    private PrdInfoLprService prdInfoLprService;
    @Autowired
    @Qualifier("sOrgService")
    private SOrgService sOrgService;
    @Autowired
    private CusIndivService cusIndivService;
    @Autowired
    private CusReferrerInfoService cusReferrerInfoService;
    @Autowired
    private AccLoanService accLoanService;
    @Autowired
    private SOrgInfoService sOrgInfoService;
}
